package com.stylefeng.guns.modular.biz.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.stylefeng.guns.common.constant.factory.ConstantFactory;
import com.stylefeng.guns.common.constant.factory.PageFactory;
import com.stylefeng.guns.common.exception.BizExceptionEnum;
import com.stylefeng.guns.common.exception.BussinessException;
import com.stylefeng.guns.common.persistence.dao.WorkNoticeMapper;
import com.stylefeng.guns.common.persistence.model.ProductInfo;
import com.stylefeng.guns.common.persistence.model.WorkNotice;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.log.LogObjectHolder;
import com.stylefeng.guns.core.shiro.ShiroKit;
import com.stylefeng.guns.core.util.ToolUtil;
import com.stylefeng.guns.modular.biz.dao.ProductDao;
import com.stylefeng.guns.modular.biz.dao.WorkNoticeDao;
import com.stylefeng.guns.modular.biz.service.IOrderService;
import com.stylefeng.guns.modular.biz.service.IWorkNoticeService;
import com.stylefeng.guns.modular.biz.warpper.ProductWarpper;
import com.stylefeng.guns.modular.biz.warpper.WorkNoticeWarpper;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * 工单通知控制器
 *
 * @author fengshuonan
 * @Date 2017-10-22 11:38:15
 */
@Controller
@RequestMapping("/workNotice")
public class WorkNoticeController extends BaseController {

    private String PREFIX = "/biz/workNotice/";

    @Resource
    private WorkNoticeDao workNoticeDao;

    @Resource
    private WorkNoticeMapper workNoticeMapper;

    @Resource
    IWorkNoticeService workNoticeService;

    /**
     * 跳转到工单通知首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "workNotice.html";
    }

    /**
     * 跳转到添加工单通知
     */
    @RequestMapping("/workNotice_add")
    public String workNoticeAdd() {
        return PREFIX + "workNotice_add.html";
    }

    /**
     * 跳转到修改工单通知
     */
    @RequestMapping("/workNotice_update/{workNoticeId}")
    public String workNoticeUpdate(@PathVariable Integer workNoticeId, Model model) {
        if (ToolUtil.isEmpty(workNoticeId)) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        WorkNotice workNotice=workNoticeMapper.selectById(workNoticeId);
        model.addAttribute("workNotice",workNotice);
        model.addAttribute("workTypeValue", ConstantFactory.me().getWorkNoticeTypeName(workNotice.getWorkType()));
        LogObjectHolder.me().set(workNotice);
        return PREFIX + "workNotice_edit.html";
    }

    /**
     * 获取工单通知列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(WorkNotice workNotice) {
        Page<WorkNotice> page = new PageFactory<WorkNotice>().defaultPage();
        Integer deptid= ShiroKit.getUser().getDeptId();//获取当前用户部门id
        if(ShiroKit.getUser().getDeptId()==27){//超级管理员
            deptid=null;
        }
        List<Map<String, Object>> result = workNoticeDao.getWorkNotices(page ,deptid,workNotice.getOrderNo(), workNotice.getUserName() ,workNotice.getUserPhone(), workNotice.getAddress(),
                workNotice.getWorkType(),workNotice.getStatus(),page.getOrderByField(), page.isAsc()) ;
        page.setRecords((List<WorkNotice>) new WorkNoticeWarpper(result).warp());
        return super.packForBT(page);
    }

    /**
     * 新增工单通知
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add() {
        return super.SUCCESS_TIP;
    }

    /**
     * 删除工单通知
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete() {
        return SUCCESS_TIP;
    }


    /**
     * 修改工单通知
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(@Valid WorkNotice workNotice, BindingResult result) {
        if (result.hasErrors()) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        if (ToolUtil.isEmpty(workNotice) || workNotice.getId() == null) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        workNoticeService.updateWorkNotice(workNotice);

        return super.SUCCESS_TIP;
    }

    /**
     * 工单通知详情
     */
    @RequestMapping(value = "/detail")
    @ResponseBody
    public Object detail() {
        return null;
    }
}
